const util = require('../../utils/util.js');
const app = getApp();
Component({
	properties: {
		initializationDate: {
			type: String,
			value: undefined
		},
		initializationChoice: {
			type: String,
			value: undefined
		},
		dropDownMenuTitle: {
			type: Array,
			value: []
		},
		choiceAllList: {
			type: Array,
			value: []
		},
		choiceTwoList: {
			type: Array,
			value: []
		},
		choiceOneList: {
			type: Array,
			value: []
		},
		timeList: {
			type: Array,
			value: []
		},
		isSite: {
			type: Boolean
		}
	},
	data: {
		// private properity
		district_open: false,
		district_open_wtapper: false,
		filter_open: false,
		filter_open_wtapper: false,// 车辆选择是否开启
		shownavindex: '',
		timeListRight: {},
		district_left_select: '',
		district_right_select: '',
		district_right_select_name: '',
		selected_year_name: '',
		selected_filter_id: 0,
		year: '',
		chooseYear: '',
		month: '',
		selected_filter_name: '',// 选择车辆选项
		choiceType: '全部选项'
	},
	methods: {
		onClickChoice (e) {
			let index = e.currentTarget.dataset.nav;
			this.selectTap(e,index);
		},
		selectTap (e, index) {
			if (parseInt(index) === 0) {
				this.tapFilterNav(e);
			} else {
				this.tapDistrictNav(e);
			}
		},
		tapDistrictNav (e) {
			let date = new Date();
			const year = date.getFullYear();
			const month = date.getMonth();
			this.setData({
				year: year,
				month: month + 1
			});
			if (!this.data.district_right_select_name) {
				if (this.data.initializationDate) {
					// 月账单推送进入
					let setData = this.data.timeList.filter(item => {
						if (item.title.includes(this.data.initializationDate.slice(0, 4))) {
							return item;
						}
					});
					this.setData({
						district_left_select: setData[0].id,
						district_right_select: setData[0].childModel[parseInt(this.data.initializationDate.slice(4, 6)) - 1].id,
						timeListRight: setData[0].childModel,
						selected_year_name: setData[0].title,
						chooseYear: parseInt(setData[0].title)
					});
				} else {
					this.setData({
						timeListRight: this.data.timeList[this.data.timeList.length - 1].childModel,
						district_left_select: this.data.timeList[this.data.timeList.length - 1].id,
						district_right_select: this.data.timeList[this.data.timeList.length - 1].childModel[month].id,
						selected_year_name: this.data.timeList[this.data.timeList.length - 1].title,
						chooseYear: parseInt(this.data.timeList[this.data.timeList.length - 1].title)
					});
				}
			}
			if (this.data.district_open) {
				this.setData({
					district_open: false,
					district_open_wtapper: false,
					filter_open: false,
					filter_open_wtapper: false,
					shownavindex: 0
				});
			} else {
				this.setData({
					district_open: true,
					district_open_wtapper: true,
					filter_open: false,
					filter_open_wtapper: false,
					shownavindex: e.currentTarget.dataset.nav
				});
			}
			this.setData({
				toview: 'year0'
			});
		},
		tapFilterNav (e) {
			if (!this.data.selected_filter_name) {
				this.setData({
					selected_filter_name: this.data.dropDownMenuTitle[0]
				});
			}
			if (this.data.filter_open) {
				this.setData({
					district_open: false,
					district_open_wtapper: false,
					filter_open: false,
					filter_open_wtapper: false,
					shownavindex: 0
				});
			} else {
				this.setData({
					district_open: false,
					district_open_wtapper: false,
					filter_open: true,
					filter_open_wtapper: true,
					shownavindex: e.currentTarget.dataset.nav
				});
			}
		},
		selectDistrictTop (e) {
			let model = e.target.dataset.model.childModel;
			let selectedId = e.target.dataset.model.id;
			let selectedTitle = e.target.dataset.model.title;
			this.setData({
				timeListRight: model == null ? '' : model,
				district_left_select: selectedId,
				district_right_select: '',
				selected_year_name: selectedTitle,
				chooseYear: parseInt(selectedTitle)
			});
			if (model == null || model.length === 0) {
				this.closeHyFilter();
				this.triggerEvent('selectedItem', { index: this.data.shownavindex, selectedId: selectedId, selectedTitle: selectedTitle });
			}
		},
		hide () {
			const isChoice = this.data.district_open_wtapper;
			this.setData({
				district_open_wtapper: false,
				filter_open_wtapper: false
			});
			setTimeout(() => {
				this.setData({
					district_open: false,
					filter_open: false
				});
			}, 400);
			if (!isChoice) {
				this.triggerEvent('selectedItem', {
					index: this.data.shownavindex,
					selectedTitle: this.data.selected_filter_name || this.data.choiceType
				});
			}
		},
		selectDistrictBottom (e) {
			let date = new Date();
			let selectedId = e.target.dataset.model.id;
			let selectedTitle = e.target.dataset.model.title;
			if (parseInt(this.data.selected_year_name) === this.data.year && parseInt(selectedTitle) > this.data.month) {
				return;
			}
			this.closeHyFilter();
			this.setData({
				district_right_select: selectedId,
				district_right_select_name: this.data.selected_year_name + selectedTitle + '月'
			});
			this.triggerEvent('selectedItem', { index: this.data.shownavindex, selectedId: selectedId, selectedTitle: selectedTitle });
		},
		selectFilterItem (e) {
			let selectedTitle = e.target.dataset.model;
			this.closeHyFilter();
			this.setData({
				selected_filter_name: selectedTitle
			});
			this.triggerEvent('selectedItem', { index: this.data.shownavindex, selectedTitle: selectedTitle });
		},
		choicechoiceType (e) {
			let choiceType = e.target.dataset.model;
			this.setData({
				choiceType
			});
		},
		/** 关闭筛选 */
		closeHyFilter () {
			if (this.data.district_open) {
				this.setData({
					district_open_wtapper: false,
					filter_open_wtapper: false,
					filter_open: false
				});
				setTimeout(() => {
					this.setData({
						district_open: false
					});
				}, 400);
			} else if (this.data.filter_open) {
				this.setData({
					district_open: false,
					district_open_wtapper: false,
					filter_open_wtapper: false
				});
				setTimeout(() => {
					this.setData({
						filter_open: false
					});
				}, 400);
			}
		}
	},
	// 组件生命周期函数，在组件实例进入页面节点树时执行
	attached () {
		this.setData({
			selected_filter_name: this.data.initializationChoice
		});
	}
});
